import { ChangeDetectorRef, Component, ElementRef, OnInit, TemplateRef, ViewChild } from '@angular/core';

import { AreaMapRightData, DashboardTop, OrganizationInfo } from '@app/core/domain/dashboard';
import { DashboardService } from '@app/core/services/http/dashboard/dashboard.service';
import { SharedModule } from '@app/shared/shared.module';
import { NgxEchartsDirective } from 'ngx-echarts';

import { AreaMapComponent } from './area-map/area-map.component';
import { CurveChartComponent } from './curve-chart/curve-chart.component';
import { IncomeChartComponent } from './income-chart/income-chart.component';
import { OrganizationInfoComponent } from './organization-info/organization-info.component';
import { PublicHealthChartComponent } from './public-health-chart/public-health-chart.component';

@Component({
  selector: 'app-dashboard',
  standalone: true,
  imports: [IncomeChartComponent, PublicHealthChartComponent, SharedModule, NgxEchartsDirective, AreaMapComponent, OrganizationInfoComponent, IncomeChartComponent, CurveChartComponent],
  templateUrl: './dashboard.component.html',
  styleUrl: './dashboard.component.less'
})
export class DashboardComponent implements OnInit {
  dataType: number = 1;
  dashboardTop!: DashboardTop;
  organizationPatientInfos: OrganizationInfo[] = [];
  currentDateTime: string = '';
  areaMapRightData!: AreaMapRightData;
  @ViewChild('incomeChart') incomeChart!: IncomeChartComponent;
  @ViewChild('healthChart') healthChart!: PublicHealthChartComponent;
  constructor(
    private readonly dashboardService: DashboardService,
    private readonly cdr: ChangeDetectorRef
  ) {}

  loadTopNumberData(dataType: number): void {
    this.dashboardService.getDashboardNumberByType(dataType).subscribe((result: DashboardTop) => {
      this.dashboardTop = result;
      this.cdr.markForCheck();
    });
  }

  loadOrganizationPatientInfo(): void {
    this.dashboardService.getOrganizationPatientInfos(this.dataType, { '@filter': 'noteq(OrganizeName,息烽县人民医院)' }).subscribe((result: OrganizationInfo[]) => {
      this.organizationPatientInfos = result.filter(r => r.organizename !== '息烽县人民医院');
    });
  }

  loadAreaMapRightData(): void {
    this.dashboardService.getAreaMapRightData(this.dataType).subscribe((result: AreaMapRightData) => {
      this.areaMapRightData = result;
    });
  }

  switchDataType(type: number): void {
    this.dataType = type;
    this.incomeChart.dataType = type;
    this.healthChart.dataType = type;
    this.incomeChart.loadIncomeData();
    this.healthChart.loadChartData();
    this.loadTopNumberData(this.dataType);
    this.loadOrganizationPatientInfo();
    this.loadAreaMapRightData();
  }

  formatDate(date: Date): string {
    const year = date.getFullYear();
    const month = date.getMonth() + 1; // 注意月份从0开始
    const day = date.getDate();
    return `${year}年${month}月${day}号`;
  }

  ngOnInit(): void {
    this.loadTopNumberData(this.dataType);
    this.currentDateTime = this.formatDate(new Date());
    this.loadOrganizationPatientInfo();
    this.loadAreaMapRightData();
  }
}
